package com.wangjie.utils;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * Bean拷贝工具类
 */
@Slf4j
public class BeanCopyUtils {
    private BeanCopyUtils() {

    }

    // 对象拷贝
    public static <T, V> T copyBean(V source, Class<T> clazz) {
        try {
            T target = clazz.newInstance();
            BeanUtils.copyProperties(source, target);
            return target;
        } catch (Exception e) {
            log.error("拷贝对象异常: ", e);
            return null;
        }
    }

    // 集合拷贝
    public static <T, V> List<T> copyBeanList(List<V> list, Class<T> clazz) {
        return list.stream()
                .map(e -> copyBean(e, clazz))
                .collect(Collectors.toList());
    }
}
